package com.hjq.fcloud.ui.activity

import com.hjq.base.BaseDialog
import com.hjq.fcloud.BuildConfig
import com.hjq.fcloud.R
import com.hjq.fcloud.app.AppActivity
import com.hjq.fcloud.config.Config
import com.hjq.fcloud.http.Urls
import com.hjq.fcloud.manager.ActivityManager
import com.hjq.fcloud.other.AppConfig
import com.hjq.fcloud.ui.activity.BrowserActivity.Companion.start
import com.hjq.fcloud.ui.dialog.MessageDialog
import com.hjq.fcloud.utils.AppConfigUtils
import kotlinx.android.synthetic.main.setting_activity.aboutSb
import kotlinx.android.synthetic.main.setting_activity.exitLoginBtn
import kotlinx.android.synthetic.main.setting_activity.versionSb

class SettingActivity : AppActivity() {

    override fun getLayoutId(): Int {
        return R.layout.setting_activity
    }

    override fun initView() {
        aboutSb.setOnClickListener {
            if(Config.IS_TEST){
                startActivity(TestActivity::class.java)
                return@setOnClickListener
            }

            start(activity, BuildConfig.HOST_URL + Urls.URL_ABOUT)
        }
        versionSb.setRightText(AppConfig.getVersionName())
        exitLoginBtn.setOnClickListener { showExitDialog() }
    }

    override fun initData() {
    }

    private fun showExitDialog() {
        // 消息对话框
        MessageDialog.Builder(activity) // 标题可以不用填写
            .setTitle(getString(R.string.string_dialog_default_title)) // 内容必须要填写
            .setMessage("确认退出?") // 确定按钮文本
            .setConfirm(getString(R.string.common_confirm)) // 设置 null 表示不显示取消按钮
            .setCancel(getString(R.string.common_cancel)) // 设置点击按钮后不关闭对话框
            //.setAutoDismiss(false)
            .setListener(object : MessageDialog.OnListener {
                override fun onConfirm(dialog: BaseDialog?) {
                    clearDataAndJumpLogin()
                }
            })
            .show()
    }

    private fun clearDataAndJumpLogin() {
        AppConfigUtils.saveToken("")
        startActivity(LoginActivity::class.java)
        // 进行内存优化，销毁除登录页之外的所有界面
        ActivityManager.getInstance().finishAllActivities(
            LoginActivity::class.java
        )
    }

}